from BaseControl import BaseWebControl, WebFindKey
import time


class WebDefaultCheckBox(BaseWebControl):
    def __init__(self, handler):
        super(WebDefaultCheckBox, self).__init__(handler)

    def setCheckState(self, state):
        def checker(driver):
            if state != self.isChecked():
                self.click()
                time.sleep(1)
            return state == self.isChecked()
        self.waitFor(checker, message="Unable to set %s state for CheckBox" % state)

    def isChecked(self):
        return self.is_selected()


class WebHorizontalCheckBox(WebDefaultCheckBox):
    def __init__(self, handler):
        super(WebHorizontalCheckBox, self).__init__(handler)

    def isChecked(self):
        checkLabel = self.waitChildControl(WebFindKey.XPATH, "..//label")
        return 'checkbox-checked' in checkLabel.get_attribute("class")

    def description(self):
        id = self.waitChildControl(WebFindKey.XPATH, "..//input").get_attribute('id')
        return id.split('_')[-1]
